 //
//  ZYStatusOriginalView.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/14.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit
import SnapKit
 
 // 子控件之间的间距
 
 let StatusTableViewCellMargin: CGFloat = 10
 
 
// 原创微博视图
class ZYStatusOriginalView: UIView {
    // 底部约束
    var originalViewBottom: Constraint?
    
    
    // MARk: 懒加载控件
    
    // 设置子控件数据
    var statusViewModel: ZYStatusViewModel? {
        didSet {
            // 设置用户头像
            if let url = statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
            
            }
            
            // 卸载约束
            originalViewBottom?.uninstall()
            
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
             pictureView.hidden = false
            pictureView.picUrls = picUrls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
            } else {
                 pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(contentLable).offset(StatusTableViewCellMargin).constraint
                })

                
            }
            
            
            
            // 设置昵称
            screenNameLable.text = statusViewModel?.status?.user?.screen_name
            // 微博内容
//            contentLable.text = statusViewModel?.status?.text
            // 设置富文本
            contentLable.attributedText = statusViewModel?.origianlAttributedString
            contentLable.numberOfLines = 0
        
            // 设置数据来源
            sourceLable.text = statusViewModel?.sourceContent
            // 设置时间
            timeLable.text = statusViewModel?.timeContent
            // 会员等级
            mbrankImageView.image = statusViewModel?.mbrankImage
            // 认证等级
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
        }
        
    }
    // 用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 昵称
    private lazy var screenNameLable: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
    // 会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 时间
    private lazy var timeLable: UILabel = UILabel(textColor: UIColor.orangeColor(), font: 12)
    // 来源
    private lazy var sourceLable: UILabel = UILabel(textColor: UIColor.lightGrayColor(), font: 12)
    
    // 微博内容
    private lazy var contentLable: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: HomeFontSize)
    
    // 配图视图
    private lazy var pictureView: ZYStatusPictureView = {
       
        let view = ZYStatusPictureView()
        return view
        
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
//        backgroundColor = RandomColor()
        //  设置背景色
        backgroundColor = UIColor.whiteColor()
        // 添加子视图
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLable)
        addSubview(mbrankImageView)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(contentLable)
        addSubview(pictureView)
        // 设置约束
        userImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        screenNameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusTableViewCellMargin)
            
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLable.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(screenNameLable)
        }
        timeLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusTableViewCellMargin)
            make.bottom.equalTo(userImageView)
        }
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLable.snp_trailing).offset(StatusTableViewCellMargin)
            make.bottom.equalTo(timeLable.snp_bottom)
        }
        contentLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
            

        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLable.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLable)
        }
        self.snp_makeConstraints { (make) -> Void in
         self.originalViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
        
    }

}

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 